using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using DeviceSolutions.SPOT.Hardware;

namespace eHouse
{
    class Alarm
    {
        private SPI spi;
        private static Alarm instance;
        byte[] writeBuffer = new byte[1];
        byte[] readBuffer = new byte[1];
        SPI.Configuration config =
            new SPI.Configuration(Meridian.Pins.SPI_SS,
            false, 0, 0, true, true, 300, SPI.SPI_module.SPI1);
		public bool alarmOn = false;

        private Alarm()
        {
            spi = new SPI(config);
           //spi = new SPI(new SPI.Configuration(
           //Cpu.Pin.GPIO_NONE, // SS-pin
           //false,             // SS-pin active state
           //0,                 // The setup time for the SS port
           //0,                 // The hold time for the SS port
           //false,              // The idle state of the clock
           //true,             // The sampling clock edge
           //15000,              // The SPI clock rate in KHz
           //SPI.SPI_module.SPI1));   // The used SPI bus (refers to a MOSI MISO and SCLK pinset)          
        }

        public static Alarm getInstance()
        {
            if (instance == null)
                instance = new Alarm();

            return instance;
        }
        public void AlarmOn()
        {
			alarmOn = true;
            //spi = new SPI(config);
            writeBuffer[0] = 0xFF;
            //spi.Write(writeBuffer);
            //writeBuffer[0] = 0x0F;
            readBuffer = new byte[1];
            for (int i = 0; i < 1000; i++)
            {
                spi.WriteRead(writeBuffer, readBuffer);
            }
            

            //Debug.Print(readBuffer[0].ToString());
            //spi.Dispose();

            //return readBuffer[0]==1;
        }

        public void switchOffAlarm()
        {
			alarmOn = false;
            writeBuffer[0] = 0x01;
            readBuffer = new byte[1];
            for (int i = 0; i < 1000; i++)
            {
                //spi.Write(writeBuffer);
                spi.WriteRead(writeBuffer, readBuffer);
            }
        }

        
    }
}
    